package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.dto.DeviceRunLogVo;
import com.workcell.workdata.xthings.entity.DeviceRunLog;
import com.workcell.workdata.xthings.mapper.DeviceRunLogMapper;
import com.workcell.workdata.xthings.service.DeviceRunLogService;
import org.springframework.stereotype.Service;

/**
* @author Admin
* @description 针对表【t_device_run_log(运行日志表)】的数据库操作Service实现
* @createDate 2023-05-08 16:41:16
*/
@Service
public class DeviceRunLogServiceImpl extends ServiceImpl<DeviceRunLogMapper, DeviceRunLog>
    implements DeviceRunLogService{
	@Override
	public Page<DeviceRunLog> vaguePage(DeviceRunLogVo.SelectDataVo selectDataVo) {
		//串口设备将厂商编码置为空
		if ("0004".equals(selectDataVo.getHardwareType())){
			selectDataVo.setManufacturerCode(null);
		}
		LambdaQueryWrapper<DeviceRunLog> deviceRunLogCondition = new QueryWrapper<DeviceRunLog>().lambda()
				.eq(ObjectUtil.isNotEmpty(selectDataVo.getHardwareType()), DeviceRunLog::getHardwareType, selectDataVo.getHardwareType())
				.eq(ObjectUtil.isNotEmpty(selectDataVo.getDeviceNo()), DeviceRunLog::getDeviceNo, selectDataVo.getDeviceNo())
				.eq(ObjectUtil.isNotEmpty(selectDataVo.getManufacturerCode()), DeviceRunLog::getManufacturerCode, selectDataVo.getManufacturerCode())
				.and(ObjectUtil.isNotEmpty(selectDataVo.getStartTime()) || ObjectUtil.isNotEmpty(selectDataVo.getEndTime())
						, i -> i.between(DeviceRunLog::getCreateTime, selectDataVo.getStartTime(), selectDataVo.getEndTime()))
				.eq(ObjectUtil.isNotEmpty(selectDataVo.getEventType()), DeviceRunLog::getEventType, selectDataVo.getEventType());
		Page<DeviceRunLog> page = new Page<>(selectDataVo.getPageNum(), selectDataVo.getPageSize());
		page.addOrder(OrderItem.desc("create_time"));
		return this.page(page, deviceRunLogCondition);
	}
}




